<?php
require_once 'Database.class.php';
/**
 * 
 * Enter description here ...
 * @author HUYHQ
 *
 */
class Liked extends Database{
	function __construct() {
		parent::__construct();
		parent::connect();
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param string: $videoId
	 * @param facebook userId: $userId
	 */
	public function checkLiked($videoId = null, $userId = null){
		
		$query = 'SELECT * 
				 FROM liked l	
				 WHERE l.video_id = "'.$videoId.'"
						AND l.user_id = "'.$userId.'"';
		$result = $this->fetchTable($query);
		//var_dump(count($result));die;
		if(count($result))
			return TRUE;
		else
			return FALSE;	
		
	}
	/**
	 * 
	 * Enter description here ...
	 * @param Facebook user $userId
	 */
	
	public function likeCounter($userId = null){
		$query = 'SELECT	SUM(`count`) count_total
				FROM liked l
				WHERE	l.user_id = "'.$userId.'"
					AND	l.like_flg = 1';
		$result = $this->fetchTable($query, 2);
		return $result[0]['count_total'];
	} 
	
	/**
	 * 
	 * Inser liked video in Liked Table
	 * @param string: $videoId
	 * @param facebook userId: $userId
	 */
	public function insertLike($videoId = NULL, $userId = NULL, $likeIncrease = 0){
	$query = 'INSERT  INTO liked(video_id, user_id, `count`, like_flg, date_created, date_updated)
					VALUES("'.$videoId.'", "'.$userId.'", '.$likeIncrease.', 1, SYSDATE(), SYSDATE())';	
		return $this->executeQuery($query);
	}
	
	public function setLikeValue($videoId = NULL, $userId = NULL, $likeValue){		
		$query = 'UPDATE  liked 
					SET like_flg = '.$likeValue.'
					WHERE liked.video_id = "'.$videoId.'"
							AND liked.user_id = "'.$userId.'"';
			$result = $this->executeQuery($query);
		}
}